﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace APPFrameworkStudy.Repository
{
    public abstract class BaseDataContext : IDataContext
    {
        //定义返回数据映射器的方法签名
        public abstract IDataMapper<T> GetDataMapper<T>() where T : class,new();

        public IQueryable<T> GetQueryable<T>() where T : class, new()
        {
            return GetDataMapper<T>().GetQueryable();
        }

        public IList<T> GetAll<T>() where T : class, new()
        {
            return GetDataMapper<T>().GetAll();
        }

        public IList<T> GetAll<T>(IQueryable<T> query) where T : class, new()
        {
            return GetDataMapper<T>().GetAll(query);
        }

        public IList<T> GetPage<T>(int pageIndex, int pageSize, out int total) where T : class, new()
        {
            return GetDataMapper<T>().GetPage(pageIndex, pageSize, out total);
        }

        public IList<T> GetPage<T>(IQueryable<T> query, int pageIndex, int pageSize, out int total) where T : class, new()
        {
            return GetDataMapper<T>().GetPage(query,pageIndex, pageSize, out total);
        }

        public IList<T> GetByCriteria<T>(Expression<Func<T, bool>> whereLambda) where T : class, new()
        {
            return GetDataMapper<T>().GetByCriteria(whereLambda);
        }

        public IList<T> GetPageByCriteria<T>(int pageIndex, int pageSize, out int total,
            Expression<Func<T, bool>> whereLambda) where T : class, new()
        {
            return GetDataMapper<T>().GetPageByCriteria(pageIndex, pageSize, out total, whereLambda);
        }

        public IList<T> GetByCriteria<T>(IQueryable<T> query, Expression<Func<T, bool>> whereLambda) where T : class, new()
        {
            return GetDataMapper<T>().GetByCriteria(query,whereLambda);
        }

        public IList<T> GetPageByCriteria<T>(IQueryable<T> query, int pageIndex, int pageSize, out int total, Expression<Func<T, bool>> whereLambda) where T : class, new()
        {
            return GetDataMapper<T>().GetPageByCriteria(query,pageIndex, pageSize, out total, whereLambda);
        }

        public T GetByKey<T>(object key) where T : class, new()
        {
            return GetDataMapper<T>().GetByKey(key);
        }

        public int GetCount<T>() where T : class, new()
        {
            return GetDataMapper<T>().GetCount();
        }

        public int GetCountByCriteria<T>(Expression<Func<T, bool>> whereLambda) where T : class, new()
        {
            return GetDataMapper<T>().GetCountByCriteria(whereLambda);
        }

        public bool Create<T>(T item) where T : class, new()
        {
            return GetDataMapper<T>().Create(item);
        }

        public bool Delete<T>(T item) where T : class, new()
        {
            return GetDataMapper<T>().Delete(item);
        }

        public bool Update<T>(T item) where T : class, new()
        {
            return GetDataMapper<T>().Update(item);
        }

        public void Dispose()
        {
            
        }


    }
}
